/**
 * @class   wosh::FilePath
 * @brief   
 * 
 *
 *
 *
 ****************************************************************************
 * @version $Id: FilePath.h 2863 2010-08-07 04:28:58Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR UserS;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Core_FilePath_H__
 #define __WOSH_Core_FilePath_H__

 #include <woshDefs.h>
 #include <core/ResourcePath.h>
 #include <string>


namespace wosh {

 #define _FileSystem_PathSeparator			"/"
 #ifdef _OS_POSIX
  #define _FileSystem_PathSeparatorLocal	"/"
 #else
  #define _FileSystem_PathSeparatorLocal	"\\"
 #endif


class FilePath : public ResourcePath
 {
	WOSH_SERIALIZABLE(wosh::FilePath)
	WOSH_COMPARE_RTTI(wosh::FilePath)

	public:
		/**
		 * @brief  Empty constructor.
		 */
		FilePath();
		/**
		 * @brief  Empty constructor with default initializer.
		 * @param [in] wosh_path the full path (omitting protocol)
		 * @see    set()
		 */
		FilePath( const std::string& wosh_path );
		/**
		 * @brief  Copy constructor.
		 * @param m [in] the source object to be copied
		 */
		FilePath( const FilePath& m );
		/**
		 * @brief  Empty destructor.
		 */
		virtual ~FilePath() { }


/** @name Getters
 * @{
 ******************************************************************************/
	public:
		/**
		 * @brief  Evaluate if the file (or folder) exists. Provided for convenience.
		 *         It will try to open the file/folder for reading.
		 * @return true if the resource can be accessed.
		 * @note   it will evaluate against internal shadow field of local path,
		 *         usually you should call resolve() and toLocalPath() before using this function
		 */
		inline bool exists() const						{ return FilePath::exists(this->pathLocal); }

		/**
		 * @brief  Evaluate if the resource is a file and exists.
		 *         It will try to open the file for reading.
		 * @return true if the resource can be accessed.
		 * @note   it will evaluate against internal shadow field of local path,
		 *         usually you should call resolve() and toLocalPath() before using this function
		 */
		inline bool isFile() const						{ return FilePath::isFile(this->pathLocal); }
		/**
		 * @brief  Evaluate if the resource is a folder and exists.
		 *         It will try to open the folder for reading.
		 * @return true if the resource can be accessed.
		 * @note   it will evaluate against internal shadow field of local path,
		 *         usually you should call resolve() and toLocalPath() before using this function
		 */
		inline bool isFolder() const					{ return FilePath::isFolder(this->pathLocal); }

		/**
		 * @brief  Get the folder path of the resource, in other words: the containing folder. 
		 * @return literal string representing the path of the folder
		 * @note   path is not resolved
		 */
		inline std::string getFolder() const			{ return FilePath::getFolder(this->path); }
		/**
		 * @brief  Get the name of the resource, in other words: filename (including extension) 
		 * @return literal string representing the file-name/folder-name
		 * @note   path is not resolved
		 */
		inline std::string getName() const				{ return FilePath::getName(this->path); }
		/**
		 * @brief  Get the file-extension of the resource.
		 * @param [in] change_case '1' will return upcase value, '2' forces to lowercase, '0' (default) does nothing
		 * @return literal string representing the file-extension (part of the path after last '.')
		 */
		inline  std::string getExtension( short change_case = 0 ) const
														{ return FilePath::getExtension(this->path, change_case); }

		// note you should USUALLY call toLocalPath() when you need the LAST UPDATED resolution
		// else you get the one evaluated on creation / set()
		inline const std::string& getPathLocal() const	{ return this->pathLocal; }
		inline std::string& getPathLocal()				{ return this->pathLocal; }

		std::string toPathLocal( bool resolve = true ) const;

//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:

		virtual bool set( const std::string& wosh_path );

		bool fromLocalPath( const std::string& local_path );

		void setPathLocal( const std::string& value )	{ this->pathLocal = value; }

//@}
/** @name Serialization interface
 * @{
 ******************************************************************************/
	public:

//@}
/** @name static methods
 * @{
 ******************************************************************************/
	public:
		/**
		 * @brief  Evaluate if the file (or folder) exists.
		 *         It will try to open the file/folder for reading.
		 * @param [in] local_path the path to be checked (local file-system format)
		 * @return true if the resource can be accessed.
		 */
		static bool exists( const std::string& local_path );

		/**
		 * @brief  Evaluate if the resource is a file and exists.
		 *         It will try to open the file for reading. (local file-system format)
		 * @param [in] local_path the file path to be checked
		 * @return true if the resource can be accessed.
		 */
		static bool isFile( const std::string& local_path );
		/**
		 * @brief  Evaluate if the resource is a folder and exists.
		 *         It will try to open the folder for reading.
		 * @param [in] local_path the folder path to be checked (local file-system format)
		 * @return true if the resource can be accessed.
		 */
		static bool isFolder( const std::string& local_path );

		/**
		 * @brief  Get the folder path of the resource, in other words: the containing folder. 
		 * @param [in] file_path the path to be checked
		 * @return literal string representing the path of the folder
		 */
		static std::string getFolder( const std::string& file_path );
		/**
		 * @brief  Get the name of the resource, in other words: filename (including extension) 
		 * @param [in] file_path the path to be checked
		 * @return literal string representing the file-name/folder-name
		 */
		static std::string getName( const std::string& file_path );

		/**
		 * @brief  Get the file-extension of the resource.
		 * @param [in] file_path the path to be checked
		 * @param [in] change_case '1' will return upcase value, '2' forces to lowercase, '0' (default) does nothing
		 * @return literal string representing the file-extension (part of the path after last '.')
		 */
		static std::string getExtension( const std::string& file_path, short change_case = 0 );

		// so we keep initial/final slash
		static void toLocalPath( const std::string& wosh_path, std::string& local_path );

		static void fixPath( const std::string& generic_path, std::string& wosh_path );

		static void fixPath( std::string& path );
		static void toLocalPath( std::string& path );
		static void resolve( std::string& path );

		static std::string combine( const std::string& path_0, const std::string& path_1 );

//@}
/** @name Internal vars
 * @{
 ******************************************************************************/
	protected:
		std::string pathLocal;	///< shadow var, DEPRECATED? TO BE REMOVED?

//@}


}; // class def

}; // namespace wosh

#endif //__WOSH_Core_FilePath_H__
